<?php

namespace addons\shopro\controller\car;

use addons\shopro\controller\Base;
use addons\shopro\model\User;
use app\admin\model\usedcar\Brand;
use app\common\model\car\CarImageModel;
use app\common\model\car\CarUserCollectModel;
use app\common\model\car\CarUserLciModel;
use fast\Random;
use think\Db;
use app\common\library\Ems;
use think\Request;

class Car extends Base
{

    protected $noNeedLogin = ['brand_index', 'index', 'car_details'];
    protected $noNeedRight = ['*'];
    protected $brandModel;
    protected $carModel;
    protected $lciModel;
    protected $carImageModel;
    protected $carUserCollectModel;

    public function __construct()
    {
        parent::__construct();
        $this->brandModel          = new Brand();
        $this->carModel            = new \app\admin\model\usedcar\Car();
        $this->lciModel            = new CarUserLciModel();
        $this->carImageModel       = new CarImageModel();
        $this->carUserCollectModel = new CarUserCollectModel();
    }

    /**
     * @menu: 汽车列表
     * @Author:男人的坏
     * @Date:2024-05-25 13:40
     */
    public function index()
    {
        $query = $this->carModel->where(['status' => 1, 'audit_status' => 1]);
        $key   = input('key');
        $sort  = input('sort', 'id');
        $order = input('order', 'desc');
        if ($key) {
            $query->where(['name' => ['like', '%'.$key.'%']]);
        }
        $car_brand_id = input('car_brand_id');
        if ($car_brand_id) {
            $query->where(['car_brand_id' => $car_brand_id]);
        }
        $type = input('type');
        if ($type) {
            $query->where(['type' => $type]);
        }
        $data = $query->order($sort, $order)->paginate();
        $this->success('ok', $data);
    }

    /**
     * @menu 获取品牌
     * @Author:男人的坏
     * @Date:2024-05-21 21:32
     */

    public function brand_index()
    {
        $order = 'initial asc';
        $key   = input('key');
        $query = $this->brandModel->where('status', '=', 1);
        if ($key) {
            $query->where(['barname|initial' => ['like', '%'.$key.'%']]);
        }
        $data = $query->cache()->group('initial')->field('initial')->order($order)->paginate(request()->param('list_rows',
            10))->each(function ($query) {
            $query->junior = $this->brandModel->where(['initial' => $query->initial])->select();
        });
        $this->success('获取成功', $data);
    }

    /**
     * @menu 二手车详情
     * @Author:男人的坏
     * @Date:2024-05-25 14:41
     */

    public function car_details()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $info               = $this->carModel->where(['id' => $ids])->find();
        $info['image_all']  = $this->carImageModel->where(['used_car_id' => $ids])->select();
        $info['is_collect'] = $this->carUserCollectModel->where(['uid' => $this->auth->id, 'car_id'=>$ids])->count();
        $this->success('ok', $info);
    }

    /**
     * @menu 发布二手车
     * @Author:男人的坏
     * @Date:2024-05-25 10:38
     */
    public function car_add()
    {
        $image                 = input('post.image');
//        $data['car_brand_id']  = input('post.car_brand_id');
//        $data['name']          = input('post.name');
//        $data['year']          = input('post.year');
//        $data['ownership_num'] = input('post.ownership_num');
//        $data['kilometres']    = input('post.kilometres');
//        $data['content']       = input('post.content');
//        $data['price']         = input('post.price');
//        $data['type']          = input('post.type');
//        $data['color']          = input('post.color');
//        if (!$image || !$data['car_brand_id'] || !$data['name'] || !$data['year'] || !$data['ownership_num'] || !$data['kilometres'] || !$data['type']) {
//            $this->error('缺少参数');
//        }
        if(!$this->auth->group_id){
            $this->error('请先成为vip');
        }
        $is_vip = db('user_group')->where(['id'=>$this->auth->group_id,'is_vip'=>1])->find();
        if(!$is_vip){
            $this->error('请先成为vip');
        }
        $data = input('post.','','trim',false);
        $image_arr = explode(',', $image);
        if (!$image_arr) {
            $this->error('图片格式不对');
        }
        if(count($image_arr) < 12){
            $this->error('图片数量必须>12张');
        }
        $ids = input('ids');
        $data['image']   = $image_arr[0];
        if($ids){
            $row = $this->carModel->where(['id'=>$ids])->find();
            if(!$row){
                $this->error('数据不存在');
            }
            $r  = $row->allowField(true)->save($data);
            $id = $ids;
            $this->carImageModel->where(['used_car_id'=>$id])->delete();
        }else{
            $data['user_id'] = $this->auth->id;
            $data['car_code'] = Random::alnum(5);
            $r               = $this->carModel->create($data);
            $id = $r->id;
        }
        if ($r) {

            foreach ($image_arr as $k => $v) {
                $this->carImageModel->create(['used_car_id' => $id, 'image' => $v, 'user_id' => $this->auth->id]);
            }
        }
        $this->success('操作成功');
    }

    /**
     * @menu 删除二手车
     * @Author:男人的坏
     * @Date:2024-06-03 09:22
     */
    public function car_del(){
        $ids = input('ids');
        $carInfo = $this->carModel->where(['id'=>$ids,'user_id'=>$this->auth->id])->find();
        if(!$carInfo){
            $this->error('数据不存在');
        }
        $carInfo->delete();
        $this->success('ok');
    }

    /**
     * @menu 我的车型
     * @Author:男人的坏
     * @Date:2024-05-25 11:28
     */

    public function user_car_list()
    {
        $key   = input('key');
        $query = $this->carModel->where(['user_id' => $this->auth->id, 'status' => 1]);
        if ($key) {
            $query->where(['name|year|kilometres' => ['like', '%'.$key.'%']]);
        }
        $car_brand_id = input('car_brand_id');
        if ($car_brand_id) {
            $query->where(['car_brand_id' => $car_brand_id]);
        }
        $data = $query->order('id desc')->field('updatetime,audit_status,status,content', true)->paginate()->each(function ($query) {
            $query->lci_num = $this->lciModel->where(['used_car_id' => $query->id])->count();
        });
        $this->success('ok', $data);
    }

}
